Forth.Starting Forth
:for new definitionsworddefined command- convention: separate name from contents by 3 spaces
CRcarriage return;EMITemit ascii char;SPACES,SPACEtsia0 DO ... LOOPCompilingtranslates definitions to dictionary form- Execution
- ANS Forth allows for 31 characters of a name to be stored
."(dot-quote) for strings inside definitions
- numbers are stored on the stack
- postfix allows using all numbers from the stack
- Accessing the stack isn't scoped: words just work
.removes and prints numbers from the stackStack underflowon trying to pop too muchGlossarylist of newly created words / meanings- Stack notation
- Words are fully compiled and don't retain references
+,-,*,/- Operators are integers; single length signed numbers
- Can have no-op words as well eg
: INCH ; /MOD( n1 n2 – rem quot )MOD( n1 n2 – rem )SWAPswitches top two stack itemsDUPduplicates it,OVERcopies second item to topROTrotates third item to top,DROPdrops top item.Snon destructive stack print2SWAP,2DUP,2OVER,2DROP
- Text interpreter always starts at the back of the dictionary
FORGETremoves most recent definition of wordMARKER <label>retains current system state
INCLUDE <filename>to grab a file- dictionary entries go to memory, source text is saved to disk
USE <filename>sets up a block( ... )act as comments, everything between () is ignoredLISTlists a disk block,LOADloads a disk block1 BLOCKcopies block 1 of open file into systemBlockdivision of disk memory with up to 1024 charsBuffertemporary storage area<CONDITION> IF <IF-TRUE> ELSE <IF-FALSE> THEN <ALWAYS EXECUTED>
- #+BEGINSRC
, <>, 0, 0>, 0<, <, > #+ENDSRC INVERTis the not used after conditionalsOR,AND?DUP
ABORT"
PAGEclear terminal screenABSabsolute value,NEGATE,MIN,MAX- Parameter Stack – standard stack used by parameters
- Return Stack --
>Rmoves value from parameter to return stackR>moves value from return to parameter stackR@copies top item from return stack to parameter stack- Processors that support hardware floating point are faster than fixed point
- Scaling operators help with fixed point
*/( n1 n2 n3 – n4 ) n1 * n2 / n3 with a double length intermediate result*/MODsimilar- Can approximate constants by pairs of integers with errors < 10-8
- Definite loop:
<LIMIT> <INDEX> DO...LOOP
U.Runsigned number print, right justifiedDO ... <INC> +LOOPuses a custom increment
- Adding random words triggers
ABORT, which also clears all stacks - Indefinite loop:
BEGIN ... <flag> UNTIL - Infinite loop:
BEGIN ... AGAIN
BEGIN ... <f> WHILE ... REPEAT
LEAVEcauses loops to immediately endQUITremoves ok at end of executionNIPdrop first item below stack2*arithmetic left shift,2/arithmetic right shift- Stack holds unsigned or signed numbers
.,prints stack value as signed numberU.prints it as unsignedUM*,UM/MOD,U<– single length unsigned opsHEX,OCTAL,DECIMALchange base- Prefix hexadecimals with a 0
- = n BASE ! ;= Change base
- Number followed by a . is converted to a double cell number
D.print double length number<# #S #> TYPE ;
HOLDinserts character at current position<# ... #>converts double length unsigned values to strings<# ... ROT SIGN #>double length signed value etc. etc.M+double length + single lengthSM/REMd1 / n1, returning n2 and n3FM/MOD,M*,M*/- Defined numbers get compiled into the dictionary, depending on defined base
VARIABLE DATE, creating variables;
- Variables are useful for values that need to be changed after the definition has been compiled
+!increment stored value- =<VALUE> CONSTANT <NAME> =
2VARIABLE,2CONSTANT,2!,2@VARIABLE <name> <x> CELLS ALLOT
FILL (addr n char -- )fills n bytesERASE (addr n -- )zeroes out n bytesaddr count DUMPto print out arrays- Can also use byte arrays:
C! ( char addr - ),C@ ( addr - ) CREATE name <value1> , <value2> , <value3> ... ,
'Identifies location in memory from dictionary, returns execution tokenEXECUTEexecutes token on stackDUMPshows raw contents- Vectored execution
- Label pointers with a
'prefix, eg.'aloha - Tick always goes to the next word in the input stream, not the definition
[']goes to the next word in the definition- Dictionary entries
- name field, link field, code/code pointer field and data field
- name: first byte defines number of characters, then an ascii rep of the name
- precedence bit: if it should be executed during compilation or compiled into the definition
- link: address of previous definition, allows for searching dictionary
- code pointer
- data field
- execution token is code pointer
- name field, link field, code/code pointer field and data field
EXITreturns execution to next higher level definition, return from subroutine- return stack keeps return addresses
QUITis the outermost loop,INTERPRETs and prints oks.- Memory map
- forth / system variables / load definitions / user dictionary (stack)
- pad stores ascii, etc. that are being manipulated
- parameter stack grows up
- TIB grows down / terminal input buffer – stores text from terminal
- return stack grows up
- user variables/buffers at the end
CPpoints to the next cell in dictionaryHEREputsCPon the stack, can be used to estimate size of stackSPprovides address of top stack locationS0contains address of cell below the empty stackTIB,#TIB,BASE,H,>IN- User variables
- forth / system variables / load definitions / user dictionary (stack)
- Vocabularies manage the dictionary
EMITASCII re on the stack and prints itTYPEprints entire string given starting address in memory